
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>SpringBoot配置基础 - Hexo</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="3. SpringBoot基础配置3.1 属性配置
SpringBoot工程配置文件默认是 application.properties,通过键值对配置对应属性


修改服务器端口
server.p,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="Hexo" type="application/atom+xml"> 
    <link rel="icon" href="/yi-mark-gitee-io/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="SpringBoot配置基础 - Hexo"/>
    <meta name="twitter:description" content="3. SpringBoot基础配置3.1 属性配置
SpringBoot工程配置文件默认是 application.properties,通过键值对配置对应属性


修改服务器端口
server.p,"/>
    
    
    
    
    <meta property="og:site_name" content="Hexo"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="SpringBoot配置基础 - Hexo"/>
    <meta property="og:description" content="3. SpringBoot基础配置3.1 属性配置
SpringBoot工程配置文件默认是 application.properties,通过键值对配置对应属性


修改服务器端口
server.p,"/>
    
<link rel="stylesheet" href="/yi-mark-gitee-io/css/diaspora.css">

<meta name="generator" content="Hexo 6.0.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Hexo</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://yi-mark.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">SpringBoot配置基础</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">SpringBoot配置基础</h1>
        <div class="stuff">
            <span>二月 04, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/yi-mark-gitee-io/tags/SpringBoot/" rel="tag">SpringBoot</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="3-SpringBoot基础配置"><a href="#3-SpringBoot基础配置" class="headerlink" title="3. SpringBoot基础配置"></a>3. SpringBoot基础配置</h1><h2 id="3-1-属性配置"><a href="#3-1-属性配置" class="headerlink" title="3.1 属性配置"></a>3.1 属性配置</h2><blockquote>
<p>SpringBoot工程配置文件默认是 application.properties,通过键值对配置对应属性</p>
</blockquote>
<ul>
<li>修改服务器端口<ul>
<li>server.port&#x3D;80</li>
</ul>
</li>
<li>关闭运行日志图标<ul>
<li>spring.main.banner-mode&#x3D;off</li>
</ul>
</li>
<li>设置日志显示等级<ul>
<li>logging.level.root&#x3D;error</li>
</ul>
</li>
</ul>
<blockquote>
<p>SpringBoot 内置属性查询: <a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties">https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties</a></p>
</blockquote>
<blockquote>
<p>SpringBoot提供了多种属性配置方式</p>
</blockquote>
<ul>
<li>application.properties</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">server.port</span>=<span class="string">80</span></span><br></pre></td></tr></table></figure>

<ul>
<li>application.yml</li>
</ul>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line">    <span class="attr">port:</span> <span class="number">81</span></span><br></pre></td></tr></table></figure>

<ul>
<li>applicaiton.yaml</li>
</ul>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line">    <span class="attr">port:</span> <span class="number">82</span></span><br></pre></td></tr></table></figure>



<blockquote>
<p>配置文件的加载优先级：.properties&gt;.yml&gt;.yaml</p>
<p>不同配置文件中相同配置按照<strong>加载优先级</strong>相互覆盖，不同配置文件中不同配置保留</p>
</blockquote>
<h2 id="3-2-yaml"><a href="#3-2-yaml" class="headerlink" title="3.2 yaml"></a>3.2 yaml</h2><blockquote>
<p>yaml(YAML Ain’t Markup Language) , 一种数据存储格式</p>
</blockquote>
<p>优点：</p>
<ul>
<li>容易阅读</li>
<li>容易与脚本语言交互</li>
</ul>
<p>YAML文件扩展名：</p>
<ul>
<li>.yml(主流)</li>
<li>.yaml</li>
</ul>
<h3 id="3-2-1语法规则"><a href="#3-2-1语法规则" class="headerlink" title="3.2.1语法规则"></a>3.2.1语法规则</h3><ul>
<li>大小写敏感</li>
<li>属性层级关系使用多行描述，每行结尾使用冒号结束</li>
<li>使用缩进表示层级关系，同层级左侧对齐，值允许使用空格（不允许使用Tab）</li>
<li>属性值前面添加空格（属性名与属性值之间使用冒号+空格作为分隔）</li>
<li># 表示注释</li>
<li>字面值表示方式</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/04/QmfxKlragTW6IwB.png" alt="image-20220204134015255"></p>
<ul>
<li>数组表示方式：在属性名书写位置的下方使用减号作为数据开始符号，每行书写一个数据，减号与数据间空格分隔</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/04/tSomYCy482l97sK.png" alt="image-20220204134119438"></p>
<p><img src="https://s2.loli.net/2022/02/04/ORnBwSL3NV6Q7zq.png" alt="image-20220204134130661"></p>
<h3 id="3-2-2-yaml数据读取"><a href="#3-2-2-yaml数据读取" class="headerlink" title="3.2.2 yaml数据读取"></a>3.2.2 yaml数据读取</h3><blockquote>
<p>使用@Value读取单个数据，属性名引用方法：${一级属性名.二级属性名}</p>
</blockquote>
<ul>
<li>代码</li>
</ul>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment"># 单一数据</span></span><br><span class="line"><span class="attr">country:</span> <span class="string">邵阳</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 对象数据（user.name默认获得了电脑的名字,更改属性名即可）</span></span><br><span class="line"><span class="attr">user:</span></span><br><span class="line">  <span class="attr">name:</span> <span class="string">懿痕</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组数据</span></span><br><span class="line"></span><br><span class="line"><span class="attr">likes:</span> [<span class="string">basketball</span>,<span class="string">pingpong</span>,<span class="string">football</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组对象</span></span><br><span class="line"></span><br><span class="line"><span class="attr">users:</span> [&#123;<span class="attr">name:</span> <span class="string">Tom</span>,<span class="attr">age:</span> <span class="number">18</span>&#125;,&#123;<span class="attr">name:</span> <span class="string">Lucy</span>, <span class="attr">age:</span> <span class="number">20</span>&#125;]</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span> <span class="comment">// Rest模式</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;user&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 读取yml数据中的单一数据</span></span><br><span class="line">    <span class="meta">@Value(&quot;$&#123;country&#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String country;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 读取对象数据</span></span><br><span class="line">    <span class="meta">@Value(&quot;$&#123;user.name&#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 读取数组数据</span></span><br><span class="line">    <span class="meta">@Value(&quot;$&#123;likes[1]&#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String like;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 读取数组对象</span></span><br><span class="line">    <span class="meta">@Value(&quot;$&#123;users[1].age&#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String age;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(country);</span><br><span class="line">        System.out.println(name);</span><br><span class="line">        System.out.println(like);</span><br><span class="line">        System.out.println(age);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Spring boot is running ...&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>结果</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/04/m91Rgj6sqKTxPtf.png" alt="image-20220204140030241"></p>
<blockquote>
<p>在配置文件中可以使用属性名引用方式引用属性</p>
</blockquote>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">baseDir:</span> <span class="string">c:\windows</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用$&#123;属性名&#125;引用数据</span></span><br><span class="line"><span class="attr">tempDir:</span> <span class="string">$&#123;baseDir&#125;\temp</span></span><br></pre></td></tr></table></figure>



<blockquote>
<p>属性值中如果出现了转义字符，需要使用双引号包裹</p>
</blockquote>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用引号包裹的字符串，其中的转移字符可以生效</span></span><br><span class="line"><span class="attr">tempDir2:</span> <span class="string">&quot;$&#123;baseDir&#125;\temp&quot;</span> <span class="comment"># \t 制表符</span></span><br></pre></td></tr></table></figure>



<h3 id="3-2-3-配置文件封装"><a href="#3-2-3-配置文件封装" class="headerlink" title="3.2.3 配置文件封装"></a>3.2.3 配置文件封装</h3><blockquote>
<p>封装全部数据到Environment对象</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用自动装配将所有的数据分装到一个对象environment中</span></span><br><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> Environment environment;</span><br><span class="line"><span class="meta">@GetMapping</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    System.out.println(environment.getProperty(<span class="string">&quot;country&quot;</span>));</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;Spring boot is running ...&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<blockquote>
<p>自定义对象封装指定数据</p>
</blockquote>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建一个类，用于封装下面的数据</span></span><br><span class="line"><span class="comment"># 由Spring帮我们去加载数据到对象，一定要告诉Spring加载这组信息</span></span><br><span class="line"><span class="comment"># 使用时候从Spring中直接获取信息使用</span></span><br><span class="line"><span class="attr">datasource:</span></span><br><span class="line">  <span class="attr">driver:</span> <span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line">  <span class="attr">url:</span> <span class="string">jdbc:mysql://localhost/db1</span></span><br><span class="line">  <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line">  <span class="attr">password:</span> <span class="string">yihen0214</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 定义数据模型封装yaml文件中对应的数据</span></span><br><span class="line"><span class="comment">// 2. 定义为被Spring管控的Bean</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="comment">// 3. 指定指定加载的数据</span></span><br><span class="line"><span class="meta">@ConfigurationProperties(prefix = &quot;datasource&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyDataSource</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String driver;</span><br><span class="line">    <span class="keyword">private</span> String url;</span><br><span class="line">    <span class="keyword">private</span> String username;</span><br><span class="line">    <span class="keyword">private</span> String password;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> MyDataSource myDataSource;</span><br></pre></td></tr></table></figure>





<h3 id="3-2-4-解决yml文件读取时中文乱码"><a href="#3-2-4-解决yml文件读取时中文乱码" class="headerlink" title="3.2.4 解决yml文件读取时中文乱码"></a>3.2.4 解决yml文件读取时中文乱码</h3><ol>
<li>找到Settings–&gt;Editor–&gt;File Encodings</li>
</ol>
<p><img src="https://s2.loli.net/2022/02/04/QIqRD6uCyNUrnlP.png" alt="image-20220204142417750"></p>
<ol start="2">
<li>修改对应文件编码</li>
</ol>
<p><img src="https://s2.loli.net/2022/02/04/LGojgqlWaefr1ZA.png" alt="image-20220204142645057"></p>
<ol start="3">
<li>修改后重新打开yml文件，将中文乱码部分重新输入</li>
</ol>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/yi-mark-gitee-io/js/plugin.js"></script>
<script src="/yi-mark-gitee-io/js/typed.js"></script>
<script src="/yi-mark-gitee-io/js/diaspora.js"></script>


<link rel="stylesheet" href="/yi-mark-gitee-io/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/yi-mark-gitee-io/photoswipe/default-skin/default-skin.css">


<script src="/yi-mark-gitee-io/photoswipe/photoswipe.min.js"></script>
<script src="/yi-mark-gitee-io/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
